package com.xintonghua.bussiness.ui.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceView;
import android.view.View;

import com.xintonghua.bussiness.util.MyUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by liqi on 2018/3/28.
 * 用戶和客戶用於簽字的view
 */

public class DrawView extends View {

    List<Point> plist;
    public DrawView(Context context) {
//        super(context);
        this(context,null);
    }
    public DrawView(Context context, @Nullable AttributeSet attrs) {
        this(context,attrs,0);
    }

    public DrawView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        plist=new ArrayList<>();
    }
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint paint=new Paint();
        paint.setColor(Color.RED);
        paint.setAntiAlias(true);
        paint.setStrokeCap( Paint.Cap.ROUND );
        for(int i=0;i<plist.size()-1;i++)
        {
            if(plist.get(i).x>0 &&plist.get(i).y>0 && plist.get(i+1).x>0&& plist.get(i+1).y>0)
            canvas.drawLine(plist.get(i).x,plist.get(i).y,plist.get(i+1).x,plist.get(i+1).y,paint);
        }
    }
    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        return super.dispatchTouchEvent(event);
    }
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if(isCanDraw)
        {
            switch (event.getAction())
            {
                case MotionEvent.ACTION_DOWN:
                    break;
                case MotionEvent.ACTION_UP:
                    plist.add(new Point(-100,-100));
                    break;
                case MotionEvent.ACTION_MOVE:
                    Log.i("aaaa","moove======>"+(int)event.getX()+(int)event.getY());
                    Point poo=new Point((int)event.getX(),(int)event.getY());
                    plist.add(poo);
                    invalidate();
                    break;
            }
            return true;
        }else {
            return super.onTouchEvent(event);
        }

    }
    /**
     * 清空画布
     */
    public void clearView()
    {
        if(!MyUtils.isNullOrEmpty(plist))
        {
            plist.clear();
        }
        invalidate();
    }

    boolean isCanDraw=true;

    public List<Point> getPlist() {
        return plist;
    }

    public void setPlist(List<Point> plist) {
        this.plist = plist;
        invalidate();
    }

    public boolean isCanDraw() {
        return isCanDraw;
    }
// 设置是否能绘画,否者点击事件不能正常使用
    public void setIsCanDraw(boolean canDraw) {
        isCanDraw = canDraw;
    }
}
